package vn.com.vega.chacha.v2;

import java.util.ArrayList;

import vn.com.vega.chacha.R;
import vn.com.vega.chacha.v2.fragment.ShowSearchHistoryFragment;
import vn.com.vega.chacha.v2.fragment.ShowSearchResultFragment;
import vn.com.vega.music.utils.Util;
import vn.com.vega.music.view.custom.BottomPlayerView;
import vn.com.vega.music.view.custom.BottomPlayerView.ShowHideBottomPlayerListener;
import vn.com.vega.music.view.custom.ChaChaDialog;
import vn.com.vega.music.view.custom.TopTaskbarView;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class SearchScreen extends ActivityBase implements OnClickListener {
	public static final String KEY_SEARCH = "search key";

	private TopTaskbarView mTopTaskbarView;
	private View viewSearchResult;
	private View viewSearchHistory;
	private TextView searchFillterCategory;
	private TextView searchFillterType;
	private View showMenuSelectCategory;
	private View showMenuSelectType;
	private ChaChaDialog dialogSelectCategoryOnline;
	private ChaChaDialog dialogSelectType;
	private SimpleContextAdapter adapterListCategory;
	private SimpleContextAdapter adapterListtype;

	private View mPlayerViewReplace;
	private BottomPlayerView mBottomPlayerView;

	private FragmentManager fragmentManager;
	private ShowSearchHistoryFragment showSearchHistoryFragment;
	private ShowSearchResultFragment showSearchResultFragment;
	// private String[] fillterCategoryOnline;
	// private String[] fillterCategoryOffline;
	// private String[] fillterType;
	public int categorySearch = ShowSearchResultFragment.SEARCH_SONG_INDEX;
	public int typeSearch = ShowSearchResultFragment.SEARCH_ONLNE;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.search_screen);
		mTopTaskbarView = (TopTaskbarView) findViewById(R.id.topTaskbarView_search_screen);
		mTopTaskbarView.changeToSearchScreen();
		mTopTaskbarView.setOnSearchListener(searchListener);
		mTopTaskbarView.showBackButton();
		init();
	}

	private void init() {
		viewSearchResult = findViewById(R.id.view_search_result);
		viewSearchHistory = findViewById(R.id.view_search_history);

		showMenuSelectCategory = findViewById(R.id.tv_type_fillter);
		showMenuSelectType = findViewById(R.id.tv_on_off_fillter);
		showMenuSelectCategory.setOnClickListener(this);
		showMenuSelectType.setOnClickListener(this);

		searchFillterCategory = (TextView) findViewById(R.id.tv_search_fillter_category);
		searchFillterType = (TextView) findViewById(R.id.tv_search_fillter_type);
		searchFillterCategory.setText(getSearchCatehoryName(typeSearch,
				categorySearch));
		searchFillterType.setText(getSearchTypeName(typeSearch));

		mPlayerViewReplace = findViewById(R.id.view_replace_bottom_player);
		mBottomPlayerView = (BottomPlayerView) findViewById(R.id.bottomPlayerView_search_screen);
		mBottomPlayerView.setShowHideListener(showHideBottomPlayerListener);

		fragmentManager = getSupportFragmentManager();

		Intent start = getIntent();
		String searchKey = start.getStringExtra(KEY_SEARCH);
		if (!Util.checkStringIsemptyOrNull(searchKey)) {
			showResult(searchKey);
			getWindow().setSoftInputMode(
					WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		} else {
			showResult("");
			showHistory();
		}
	}

	ShowHideBottomPlayerListener showHideBottomPlayerListener = new ShowHideBottomPlayerListener() {

		@Override
		public void onShowBottomPlayer() {
			// TODO Auto-generated method stub
			mPlayerViewReplace.setVisibility(View.VISIBLE);
		}

		@Override
		public void onHideBottomPlayer() {
			// TODO Auto-generated method stub
			mPlayerViewReplace.setVisibility(View.GONE);
		}
	};

	private String getSearchCatehoryName(int searchType, int category) {
		if (searchType == ShowSearchResultFragment.SEARCH_ONLNE) {
			// online search
			switch (category) {
			case ShowSearchResultFragment.SEARCH_SONG_INDEX:
				return getString(R.string.fillter_music);
			case ShowSearchResultFragment.SEARCH_VIDEO_INDEX:
				return getString(R.string.fillter_video);
			case ShowSearchResultFragment.SEARCH_ALBUM_INDEX:
				return getString(R.string.fillter_album);
			case ShowSearchResultFragment.SEARCH_ARTIST_INDEX:
				return getString(R.string.fillter_artist);
			case ShowSearchResultFragment.SEARCH_PLAYLIST_INDEX:
				return getString(R.string.fillter_playlist);
			default:
				return getString(R.string.fillter_music);
			}
		} else {
			// offline search
			switch (category) {
			case ShowSearchResultFragment.SEARCH_SONG_INDEX:
				return getString(R.string.fillter_music);

			default:
				return getString(R.string.fillter_music);
			}

		}
	}

	private String getSearchTypeName(int searchType) {
		if (searchType == ShowSearchResultFragment.SEARCH_ONLNE) {
			return getString(R.string.fillter_online);
		} else {
			return getString(R.string.fillter_offline);
		}
	}

	private ArrayList<String> getListTypeSearch() {
		ArrayList<String> result = new ArrayList<String>();
		result.add(getSearchTypeName(ShowSearchResultFragment.SEARCH_ONLNE));
		if (categorySearch <= ShowSearchResultFragment.SEARCH_SONG_INDEX) {
			result.add(getSearchTypeName(ShowSearchResultFragment.SEARCH_OFF_LINE));
		}

		return result;
	}

	private ArrayList<String> getListCategory(int searchType) {
		ArrayList<String> listCategory = new ArrayList<String>();
		if (searchType == ShowSearchResultFragment.SEARCH_ONLNE) {
			listCategory.add(getSearchCatehoryName(
					ShowSearchResultFragment.SEARCH_ONLNE,
					ShowSearchResultFragment.SEARCH_SONG_INDEX));
			listCategory.add(getSearchCatehoryName(
					ShowSearchResultFragment.SEARCH_ONLNE,
					ShowSearchResultFragment.SEARCH_VIDEO_INDEX));
			listCategory.add(getSearchCatehoryName(
					ShowSearchResultFragment.SEARCH_ONLNE,
					ShowSearchResultFragment.SEARCH_ALBUM_INDEX));
			listCategory.add(getSearchCatehoryName(
					ShowSearchResultFragment.SEARCH_ONLNE,
					ShowSearchResultFragment.SEARCH_PLAYLIST_INDEX));
			listCategory.add(getSearchCatehoryName(
					ShowSearchResultFragment.SEARCH_ONLNE,
					ShowSearchResultFragment.SEARCH_ARTIST_INDEX));
		} else {
			listCategory.add(getSearchCatehoryName(
					ShowSearchResultFragment.SEARCH_OFF_LINE,
					ShowSearchResultFragment.SEARCH_SONG_INDEX));
		}

		return listCategory;
	}

	private void showHistory() {
		viewSearchResult.setVisibility(View.GONE);
		viewSearchHistory.setVisibility(View.VISIBLE);
		if (showSearchHistoryFragment == null) {
			showSearchHistoryFragment = new ShowSearchHistoryFragment();
			FragmentTransaction transaction = fragmentManager
					.beginTransaction();
			transaction.add(viewSearchHistory.getId(),
					showSearchHistoryFragment);
			transaction.commit();
		}
	}

	private void showResult(String searchKey) {
		viewSearchResult.setVisibility(View.VISIBLE);
		viewSearchHistory.setVisibility(View.GONE);
		mTopTaskbarView.getSearchBox().setText(searchKey);
		if (showSearchResultFragment == null) {
			showSearchResultFragment = new ShowSearchResultFragment(searchKey,
					categorySearch, typeSearch);
			FragmentTransaction transaction = fragmentManager
					.beginTransaction();
			transaction.add(viewSearchResult.getId(), showSearchResultFragment);
			transaction.commit();
		}
	}

	SearchListener searchListener = new SearchListener() {

		@Override
		public void onSearch(String key) {
			// TODO Auto-generated method stub
			doSearch(mTopTaskbarView.getSearchBox().getText().trim());
		}
	};

	public void doSearch(String key) {
		if (Util.checkStringIsemptyOrNull(key)) {
			return;
		}
		// showSearchResultFragment.doSearch(key, category, type);
		if (showSearchResultFragment != null) {
			showSearchResultFragment.setSearchKey(key);
			showResult(key);
		} else {
			showResult(key);
		}
	}

	public interface SearchListener {
		public void onSearch(String key);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_type_fillter:
			showMenuSelectCategory();
			break;
		case R.id.tv_on_off_fillter:
			showMenuSelectType();
			break;

		default:
			break;
		}
	}

	private void showMenuSelectCategory() {
		if (dialogSelectCategoryOnline != null) {
			dialogSelectCategoryOnline.show();
			return;
		}
		// Create and show dialog select category
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		ListView listCategory = (ListView) inflater.inflate(
				R.layout.view_fillter_poup, null);
		listCategory.setCacheColorHint(Color.TRANSPARENT);
		adapterListCategory = new SimpleContextAdapter(this,
				getListCategory(typeSearch));
		listCategory.setOnItemClickListener(selectCategoryClickListener);
		listCategory.setAdapter(adapterListCategory);
		dialogSelectCategoryOnline = new ChaChaDialog(this, null);
		dialogSelectCategoryOnline.hideHeader();
		dialogSelectCategoryOnline.hideBottomView();
		dialogSelectCategoryOnline.setCancelable(true);
		dialogSelectCategoryOnline.setContentView(listCategory);
		dialogSelectCategoryOnline.show();
	}

	AdapterView.OnItemClickListener selectCategoryClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			dialogSelectCategoryOnline.dismiss();
			switch (arg2) {
			case 0:
				// search Song
				categorySearch = ShowSearchResultFragment.SEARCH_SONG_INDEX;
				break;
			case 1:
				// search Video
				categorySearch = ShowSearchResultFragment.SEARCH_VIDEO_INDEX;
				break;
			case 2:
				// search Album
				categorySearch = ShowSearchResultFragment.SEARCH_ALBUM_INDEX;
				break;
			case 3:
				// search playlist
				categorySearch = ShowSearchResultFragment.SEARCH_PLAYLIST_INDEX;
				break;
			case 4:
				// search Artist
				categorySearch = ShowSearchResultFragment.SEARCH_ARTIST_INDEX;
				break;

			default:
				break;
			}

			searchFillterCategory.setText(getSearchCatehoryName(typeSearch,
					categorySearch));
			if (adapterListtype != null) {
				adapterListtype.setListItem(getListTypeSearch());
				adapterListtype.notifyDataSetChanged();
			}
			showSearchResultFragment.setSearchCategory(categorySearch);
		}
	};

	AdapterView.OnItemClickListener selectTypeClickListener = new AdapterView.OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			dialogSelectType.dismiss();
			if (arg2 == 0) {
				typeSearch = ShowSearchResultFragment.SEARCH_ONLNE;
			} else {
				typeSearch = ShowSearchResultFragment.SEARCH_OFF_LINE;
			}
			searchFillterType.setText(getSearchTypeName(typeSearch));
			if (adapterListCategory != null) {
				adapterListCategory.setListItem(getListCategory(typeSearch));
				adapterListCategory.notifyDataSetChanged();
			}
			showSearchResultFragment.setSearchType(typeSearch);

		}
	};

	private void showMenuSelectType() {
		if (dialogSelectType != null) {
			dialogSelectType.show();
			return;
		}
		// Create dialog
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		ListView listType = (ListView) inflater.inflate(
				R.layout.view_fillter_poup, null);
		adapterListtype = new SimpleContextAdapter(this, getListTypeSearch());
		listType.setAdapter(adapterListtype);
		listType.setCacheColorHint(Color.TRANSPARENT);
		listType.setOnItemClickListener(selectTypeClickListener);
		dialogSelectType = new ChaChaDialog(this, null);// .Builder(this).create();
		dialogSelectType.hideHeader();
		dialogSelectType.hideBottomView();
		dialogSelectType.setCancelable(true);
		dialogSelectType.setContentView(listType);
		dialogSelectType.show();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mBottomPlayerView.startUpdate();
	}

	@Override
	protected void onPause() {
		super.onPause();
		mBottomPlayerView.stopUpdate();
	}

	/**
	 * Simple adapter for context menu
	 * 
	 * @author Vega Corp
	 * 
	 */
	public static class SimpleContextAdapter extends BaseAdapter {
		ArrayList<String> listItem;
		Context context;
		LayoutInflater inflater;

		public void setListItem(ArrayList<String> listItem) {
			this.listItem = listItem;
		}

		public SimpleContextAdapter(Context ctx, ArrayList<String> list) {
			listItem = list;
			context = ctx;
			inflater = (LayoutInflater) ctx
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listItem.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return listItem.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder;
			if (convertView == null) {
				convertView = inflater
						.inflate(R.layout.item_fillter_poup, null);
				holder = new ViewHolder();
				holder.text = (TextView) convertView
						.findViewById(R.id.tv_fillter_label);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			String string = (String) getItem(position);
			holder.text.setText(string);
			return convertView;
		}

		static class ViewHolder {
			TextView text;
		}
	}
}
